﻿using System;

namespace Alex.Library.Tasks
{
    public interface ITask: IDisposable
    {
        /// <summary>
        /// 获取任务ID
        /// </summary>
        string ID { get; }

        /// <summary>
        /// 获取任务描述
        /// </summary>
        string Description { get; }

        /// <summary>
        /// 获取任务是否异步
        /// </summary>
        bool IsAsync { get; }

        /// <summary>
        /// 获取累计执行时长
        /// </summary>
        TimeSpan Runtimes { get; }

        /// <summary>
        /// 获取或设置任务执行的超时时间(单位：毫秒)，如果不设置超时时间则默认为TaskTimeout.Forever
        /// </summary>
        double Timeout { get; set; }

        /// <summary>
        /// 获取任务状态
        /// </summary>
        TaskState State { get; }

        /// <summary>
        /// 获取是否有错误
        /// </summary>
        bool HasError { get; }

        /// <summary>
        /// 获取错误信息
        /// </summary>
        string ErrorInfo { get; }

        /// <summary>
        /// 获取或设置任务是否允许重复执行
        /// </summary>
        bool CanRepeat { get; set; }

        /// <summary>
        /// 执行任务
        /// </summary>
        /// <param name="rollbackIfError">如果发生错误是否回滚</param>
        void Run(bool rollbackIfError);

        /// <summary>
        /// 取消任务
        /// </summary>
        /// <param name="needRollback">取消任务后是否回滚</param>
        void Cancel(bool needRollback);
    }
}
